// LSL script generated: RLV-New.Addons.TimerPLugin.lslp Sun May  2 00:09:51 Westeuropäische Sommerzeit 2010

integer attachment = 1;
list gMenu = [];
key ToucherID;
integer channel_dialog;
integer listen_id;
list gTimerMenuL = ["+5 Min","+10 Min","+30 Min","+1 H","+5 H","+10 H","+1 D","Stop","Reset","Start","Back"];
integer gTimeleft = 0;
integer gTimerMenu = 0;
integer GLockTimer;
string msgTimer = "Set a Time the Sub get's locked";

string getHoursMinutes(integer seconds){
    integer gsec = seconds;
    integer hours = 0;
    integer minutes = 0;
    integer months = 0;
    integer days = 0;
    integer years = 0;
    string returnValue = "";
    (years = (seconds / 31536000));
    (seconds = (seconds - (years * 31536000)));
    (months = (seconds / 2628000));
    (seconds = (seconds - (months * 2628000)));
    (days = (seconds / 86400));
    (seconds = (seconds - (days * 86400)));
    (hours = (seconds / 3600));
    (seconds = (seconds - (hours * 3600)));
    (minutes = (seconds / 60));
    (seconds = (seconds - (minutes * 60)));
    if (years) (returnValue = (((string)years) + "Yr. "));
    if (days) (returnValue = ((returnValue + ((string)days)) + "Day. "));
    if (hours) (returnValue = ((returnValue + ((string)hours)) + "Hr. "));
    if (minutes) (returnValue = ((returnValue + ((string)minutes)) + "Min. "));
    if (seconds) (returnValue = ((returnValue + ((string)seconds)) + "Sec."));
    return returnValue;
}

makeMenu(){
    (gMenu = []);
    (gMenu += gTimerMenuL);
}

displayMenu(){
    (channel_dialog = (-1 * ((integer)("0x" + llGetSubString(((string)llGetKey()),-5,-1)))));
    (listen_id = llListen(channel_dialog,"",NULL_KEY,""));
    (msgTimer = ("Time left:" + getHoursMinutes(gTimeleft)));
    llDialog(ToucherID,msgTimer,gMenu,channel_dialog);
}

startTimer(){
    (GLockTimer = 1);
}

endTimer(){
    (gTimeleft = 0);
    (GLockTimer = 0);
    llMessageLinked(-4,1339,"RESET",ToucherID);
}

stoptimer(){
    (GLockTimer = 0);
}

setTimer(integer Min){
    (gTimeleft += (Min * 60));
    (msgTimer = ("Time left:" + getHoursMinutes(gTimeleft)));
}

default {

    state_entry() {
        llSetTimerEvent(1.0);
        makeMenu();
    }

    
    listen(integer channel,string name,key id,string message) {
        if ((channel == channel_dialog)) {
            llListenRemove(listen_id);
            if ((message == "+5 Min")) {
                setTimer(5);
                displayMenu();
                (gTimerMenu = 45);
                (gTimerMenu = 1);
            }
            else  if ((message == "+10 Min")) {
                setTimer(10);
                displayMenu();
                (gTimerMenu = 45);
                (gTimerMenu = 1);
            }
            else  if ((message == "+30 Min")) {
                setTimer(30);
                displayMenu();
                (gTimerMenu = 45);
                (gTimerMenu = 1);
            }
            else  if ((message == "+1 H")) {
                setTimer(60);
                displayMenu();
                (gTimerMenu = 45);
                (gTimerMenu = 1);
            }
            else  if ((message == "+5 H")) {
                setTimer(300);
                displayMenu();
                (gTimerMenu = 45);
                (gTimerMenu = 1);
            }
            else  if ((message == "+10 H")) {
                setTimer(600);
                displayMenu();
                (gTimerMenu = 45);
                (gTimerMenu = 1);
            }
            else  if ((message == "+1 D")) {
                setTimer(1440);
                displayMenu();
                (gTimerMenu = 45);
                (gTimerMenu = 1);
            }
            else  if ((message == "Stop")) {
                (gTimerMenu = 45);
                (gTimerMenu = 1);
                stoptimer();
                displayMenu();
            }
            else  if ((message == "Reset")) {
                (gTimeleft = 0);
                (gTimerMenu = 45);
                (gTimerMenu = 1);
                stoptimer();
                displayMenu();
            }
            else  if ((message == "Start")) {
                (gTimerMenu = 45);
                (gTimerMenu = 1);
                startTimer();
                displayMenu();
            }
            if ((message == "Back")) llMessageLinked(-4,1337,"DV_BASIC",ToucherID);
        }
    }

    
    on_rez(integer on) {
    }

    
    link_message(integer sender,integer num,string str,key id) {
        if ((num == 1337)) {
            if ((str == llGetScriptName())) {
                llMessageLinked(-4,1338,llGetScriptName(),NULL_KEY);
                (ToucherID = id);
                displayMenu();
            }
        }
        if ((num == 1338)) {
            if ((str == "attachment")) (attachment = 1);
            else  if ((str == "device")) (attachment = 0);
            else  if ((str != llGetScriptName())) {
                llListenRemove(listen_id);
            }
        }
        if ((num == 1339)) {
            if ((str == "ON_REZ")) {
            }
            if ((str == "RESET")) {
                (gTimeleft = 0);
                (GLockTimer = 0);
            }
        }
    }

  	
  	timer() {
        if ((GLockTimer == 1)) {
            (gTimeleft = (gTimeleft - 1));
            if ((gTimeleft == 0)) {
                endTimer();
            }
        }
        
    }
}
